#include "GetMyDevice.h"
#include "db/DBManager.h"

#include <QDebug>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QJsonObject>
#include <QJsonArray>
#include <QSqlError>


void GetMyDevice::handleSession()
{
    qDebug() << "GetMyDevice requestUrlQuery: " << m_session->requestUrlQuery();

    QMap<QString, QString> mapQuery = m_session->requestUrlQuery();

    QString strOpenId = mapQuery.value("openid");

    QSqlDatabase db = DBManager::database("core_config");

    QString strSql = QString("select szDeviceName as deviceName, dtCreateDate as timeAdded from manager_device where szOpenid='%1'").arg(strOpenId);

    QSqlQuery query(db);

    if(!query.exec(strSql))
    {
        qDebug()<<"exec select user_info error, "<<query.lastError().text();
        return;
    }

    QVariantList varlist;
    while(query.next())
    {
        QVariantMap mapUserInfo;
        QSqlRecord record = query.record();
        for(int i=0; i<record.count(); i++)
        {
            mapUserInfo.insert(record.fieldName(i), record.value(i));
        }

        varlist << mapUserInfo;
    }

    m_session->replyJsonArray(QJsonArray::fromVariantList(varlist));
}
